package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MerchantOrderItem;
import com.rockcent.mall.lib.domain.enumclass.MerchantOrderSourceType;
import com.rockcent.mall.lib.dto.MerchantOrderItemDto;
import com.rockcent.mall.lib.dto.ProductDto;
import com.rockcent.mall.lib.dto.SettlementRecordDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/31.
 */
@Repository
public class MerchantOrderItemRepositoryImpl implements MerchantOrderItemRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MerchantOrderItemRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MerchantOrderItemDto> findIncomeCollectInfoPage(Long productId, Long mallId, Long merchantId, PageResult page) {
        SQLPageDoing<MerchantOrderItemDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from merchant_order_item moi where 1=1 ");
            sql.append(" and moi.merchant_id = :merchantId");
            sql.append(" and moi.mall_id = :mallId");
            sql.append(" and moi.product_id = :productId");
            sql.append(" and moi.settle_status <> 'WAIT_SETTLE'");
            params.put("productId", productId);
            params.put("mallId", mallId);
            params.put("merchantId", merchantId);
            sql.append(" order by moi.date_created desc");
            fields.append("moi.*");
        };
        return doing.go(page, MerchantOrderItemDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<MerchantOrderItem> queryAllCanCancelMerchantOrders(String productRefundable) {

        SQLListDoing<MerchantOrderItem> doing = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select %s from merchant_order o,merchant_order_item i");
            sql.append(" where o.id = i.merchant_order_id");
            sql.append(" and o.`order_type`='SERVICE'");
            sql.append(" and o.is_delete='NO'");
            sql.append(" and i.is_delete='NO'");
            sql.append(" and o.`status` in('WAIT_USE','PART_USE') and o.gift_packet_id is null");
            sql.append(" and i.merchant_role in('SELF','SUPPLIER')");
            sql.append(" and date_format(i.product_date_disabled,'%Y-%m-%d %H:%i') <= date_format(NOW(),'%Y-%m-%d %H:%i')");
            sql.append(" and i.product_refundable =:productRefundable");
            params.put("productRefundable", productRefundable);
            fields.append("i.*");
        };

        return doing.go(MerchantOrderItem.class, jdbcTemplate, log, objectMapper);

    }

    @Override
    public List<MerchantOrderItemDto> findMerchantOrderItemList(Long merchantOrderId) {
        SQLListDoing<MerchantOrderItemDto> doing = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select %s from merchant_order_item i where 1=1 ");
            sql.append(" and i.merchant_order_id = :merchantOrderId");
            params.put("merchantOrderId", merchantOrderId);
            fields.append("i.*");
        };

        return doing.go(MerchantOrderItemDto.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public Long countSaleProductCountByMerchantId(Long merchantId) {
        SQLObjectDoing<Long> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select sum(buy_count) from  merchant_order_item pe");
            sql.append(" where merchant_role <> 'SUPPLIER' and pe.merchant_id=:merchantId");
            sql.append(" and datediff(curdate(),last_updated)<90 ");
            params.put("merchantId", merchantId);

        };
        return result.go(Long.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public Double countSaleTotalAmoutByMerchantId(Long merchantId) {
        SQLObjectDoing<Double> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select sum(discount_amount) from  merchant_order pe");
            sql.append(" where merchant_role <> 'SUPPLIER' and pe.merchant_id=:merchantId");
            sql.append(" and datediff(curdate(),last_updated)<90 ");
            params.put("merchantId", merchantId);

        };
        return result.go(Double.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public Long countSaleOrderAmountByMerchantId(Long merchantId) {

        SQLObjectDoing<Long> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select count(1) from  merchant_order pe");
            sql.append(" where merchant_role <> 'SUPPLIER' and pe.merchant_id=:merchantId");
            sql.append(" and datediff(curdate(),last_updated)<90 ");
            params.put("merchantId", merchantId);
            sql.append(" and pe.merchant_role in('SELF','DISTRIBUTOR')");
//            sql.append(" and date_format(pe.product_date_disabled,'%Y-%m-%d %H:%i') <= date_format(NOW(),'%Y-%m-%d %H:%i')");
        };
        return result.go(Long.class, jdbcTemplate, log, objectMapper);

    }

    @Override
    public MerchantOrderItem caculateSaleCountAndVerifyCountByMallIdAndMerchantIdAndProductId(Long mallId, Long merchantId, Long productId) {
        SQLObjectDoing<MerchantOrderItem> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select sum(buy_count) as buy_count ,sum(verify_count) as verify_count from merchant_order_item m ,order_event o ");
            sql.append(" where m.merchant_order_num=o.merchant_order_num and o.event_type in('PAYED','VERIFY_CONFIRM')");
            sql.append(" and m.product_id=:productId ");
            sql.append(" and m.merchant_id=:merchantId ");
            sql.append(" and m.mall_id=:mallId ");
            sql.append(" and o.event_type in('PAYED','VERIFY_CONFIRM') ");
            sql.append(" group by o.event_type ");
            sql.append(" limit 1 ");
            params.put("productId", productId);
            params.put("merchantId", merchantId);
            params.put("mallId", mallId);
        };
        return result.go(MerchantOrderItem.class, jdbcTemplate, log, objectMapper);
    }
}
